<?php
/**
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
class action_handler_parser
{
	private $semf_root;
	private $semantic;
	private $components;
	public function __construct($semf_root, $semantic) 
	{		
		if($semf_root == NULL)
		{
			$this->semf_root = "../../../";
		}
		else
		{
			$this->semf_root = $semf_root;
		}
		include_once( $semf_root."page_designer/script/script.php");
		include_once( $semf_root."page_designer/util/component_loader.php");
		
		$this->components = load_components($semf_root);
		foreach($this->components as $component)
		{
			include_once($semf_root."page_designer/components/".$component."/component.php");
		}
		$this->semantic = $semantic;
	}	
	
	public function parse($properties)
	{
		$ids = array_keys($properties);
		foreach( $ids as $id)
		{
			$type = $properties[$id]['type'];
			$data_binding_properties = $properties[$id]['properties']['Action Handlers'];
			
			if( $data_binding_properties == NULL )
			{
				continue;
			}
			$this->parse_component($id, $type, $data_binding_properties);
		}
	}
	
	private function parse_component($id, $type, $properties)
	{
		//print_r($properties);
		$property_keys = array_keys($properties);
		$script = NULL;
		
		foreach ($property_keys as $key)
		{
			//print( $properties[$key]);
			//$script = new script($properties[$key], $this->semf_root);
			
			$description = "<h3>Actions of ".$type." for ".$key."</h3>";
			$project_home = $this->semantic->get_project_home();
		
			$script = 
				new script($properties[$key], $this->semf_root, $project_home, $description);
			$parse_tree = $script->get_parse_tree();
			
			$tokens = $script->get_tokens();
			$this->semantic->evaluate_action_statement($parse_tree,
					 $tokens, $id, $key, $description);
			
		}

		$comp_class_name = ${type}."Component";
		$obj = new ${comp_class_name}();
		$action_list = $obj->get_action_info($id);
		
		$this->semantic->associate_action_list($action_list, $id);
	}
}
?>